#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
	struct stat file_stat;
	struct tm file_tm;
	char time_str[100];
	int ret;

	/* 获取文件属性 */
	ret = stat("./test_file", &file_stat);
	if(-1 == ret)
	{
		perror("stat error");
		exit(-1);
	}

	/* 打印文件大小和 inode 编号 */
	printf("file size: %ld bytes\n""inode number: %ld\n", file_stat.st_size,file_stat.st_ino);

	/* 判读文件类型 */
	switch (file_stat.st_mode & S_IFMT)
	{
		case S_IFSOCK: printf("socket"); break;
		case S_IFLNK: printf("symbolic link"); break;
		case S_IFREG: printf("regular file"); break;
		case S_IFBLK: printf("block device"); break;
		case S_IFDIR: printf("directory"); break;
		case S_IFCHR: printf("character device"); break;
		case S_IFIFO: printf("FIFO"); break;
	}
	printf("\n");

	/* 判断该文件对其它用户是否具有读权限 */
	if(file_stat.st_mode & S_IROTH)
		printf("Read: Yes\n");
	else
		printf("Read: No\n");
	
	/* 判断该文件对其它用户是否具有写权限 */
	if(file_stat.st_mode & S_IWOTH)
		printf("Write: Yes\n");
	else
		printf("Write: No\n");

	/* 打印文件最后被访问的时间 */
	localtime_r(&file_stat.st_atim.tv_sec, &file_tm);
	strftime(time_str, sizeof(time_str),"%Y-%m-%d %H:%M:%S", &file_tm);
	printf("time of last access: %s\n", time_str);
	/* 打印文件内容最后被修改的时间 */
	localtime_r(&file_stat.st_mtim.tv_sec, &file_tm);
	strftime(time_str, sizeof(time_str),"%Y-%m-%d %H:%M:%S", &file_tm);
	printf("time of last modification: %s\n", time_str);
	/* 打印文件状态最后改变的时间 */
	localtime_r(&file_stat.st_ctim.tv_sec, &file_tm);
	strftime(time_str, sizeof(time_str),"%Y-%m-%d %H:%M:%S", &file_tm);
	printf("time of last status change: %s\n", time_str);

	exit(0);
}
